include("static/js/config.js");
include("static/js/common.js");

$(function () {

    var element;
    layui.use(['element'], function(){
        element = layui.element;
        load();
    });

    function load() {
        var token = window.localStorage.getItem('token');
        if (!token){
            if (window != top){
                top.location.href = config.url + "/page/login.html";
            } else {
                window.location.href = config.url + "/page/login.html";
            }
            return;
        }

        var username = window.localStorage.getItem('username');
        if (username) {
            $("#username").html(username);
        }


        var menuStr = window.localStorage.getItem('menu');
        if (menuStr){
            var html = '';
            var list = JSON.parse(menuStr);
            if (list){
                for (var i = 0; i < list.length; i++){
                    var item = list[i];
                    html += '<li class="layui-nav-item">';
                    if (item.children){
                        html += '<a class="" href="javascript:;"><i class="iconfont">' + item.icon + '</i><cite>' + item.name + '</cite></a>';
                        html += '<dl class="layui-nav-child">';
                        for (var j = 0; j < item.children.length; j++){
                            html += '<dd><a href="#" data-url="' + item.children[j].page + '">' + item.children[j].name + '</a></dd>';
                        }
                        html += '</dl>';
                    }
                    html += '</li>';
                }
                $('#LAY-system-side-menu').append(html);
                element.render();
            }
        }
    }


    $("#logout").click(function () {
        window.localStorage.removeItem("menu");
        window.localStorage.removeItem("token");
        window.localStorage.removeItem("username");
        if (window != top){
            top.location.href = config.url + "/page/login.html";
        } else {
            window.location.href = config.url + "/page/login.html";
        }
    });

    $("#LAY-system-side-menu").on('click', '.layui-nav-child a', function () {
        var $this = $(this);
        var url = $this.data("url");
        $("#page").attr("src", config.url + url);
    });


    var show = true;
    $('.kit-side-fold').click(function(){
        show = !show;
        if (show){
            showMenu();
        } else {
            hideMenu();
        }
    });

    $('.layui-side-menu li').click(function () {
        show = true;
        showMenu();
    });


    var fullscreen = false;
    $('.fullscreen').click(function () {
        fullscreen = !fullscreen;
        if (fullscreen){
            entryFullScreen();
        } else {
            exitFullScreen();
        }
    })

});


function hideMenu() {
    $('.layui-logo').css('width', '60px');
    $('.layui-logo').find('span').html('wms');
    $('.layui-header .layui-layout-left').css('left', '60px');

    $('.layui-nav-tree').css('width', '60px');
    $('.layui-side-menu').css('width', '60px');
    $('.layui-side-scroll').css('width', '80px');

    $('.layui-side-menu li').removeClass('layui-nav-itemed');
    $('.layui-nav-item cite').hide();
    $('.layui-nav-item span').hide();
}

function showMenu() {
    $('.layui-logo').css('width', '200px');
    $('.layui-logo').find('span').html('后台管理系统');
    $('.layui-header .layui-layout-left').css('left', '200px');

    $('.layui-nav-tree').css('width', '200px');
    $('.layui-side-menu').css('width', '200px');
    $('.layui-side-scroll').css('width', '220px');

    $('.layui-nav-item cite').show();
    $('.layui-nav-item span').show();
}


// 进入全屏：
function entryFullScreen() {
    var docE = document.documentElement;
    if (docE.requestFullScreen) {
        docE.requestFullScreen();
    } else if (docE.mozRequestFullScreen) {
        docE.mozRequestFullScreen();
    } else if (docE.webkitRequestFullScreen) {
        docE.webkitRequestFullScreen();
    }

    $(this).find('i').addClass('layui-icon-screen-restore');
    $(this).find('i').removeClass('layui-icon-screen-full');
}

// 退出全屏
function exitFullScreen() {
    var docE = document;
    if (docE.exitFullscreen) {
        docE.exitFullscreen();
    } else if (docE.mozCancelFullScreen) {
        docE.mozCancelFullScreen();
    } else if (docE.webkitCancelFullScreen) {
        docE.webkitCancelFullScreen();
    }

    $(this).find('i').addClass('layui-icon-screen-full');
    $(this).find('i').removeClass('layui-icon-screen-restore');
}